//app.js
import req from './utils/req.js';
import utils from './utils/utils.js'
App({
  config: {
    url: "http://localhost:88",
    appid: "wxaa8c5bfd3b2d3a20",
    version: "1.0.0"
  },
  onLaunch: function () {
    console.log(this.globalData.userInfo);
    if (this.globalData.userInfo === '') {
      wx.navigateTo({
        url: 'pages/login/login',
      })
    } else {
      this.login()
    }
  },
  login() {
    let data = {}
    wx.login({
      success: (res) => {
        let code = res.code
        wx.getUserInfo({
          success: (res) => {
            const userInfo = res.userInfo
            let name = userInfo.nickName
            let sex = userInfo.gender
            let avatar = userInfo.avatarUrl
            data = {
              code,
              name,
              sex,
              avatar
            }
            req.post('登陆中', '/user/login', data, (res) => {
              this.globalData.userInfo = res.data
              utils.toast('success', res.msg)
            })
          }
        })
      },
      fail: (res) => {
        utils.toast('error', '登陆失败')
      },
      complete: (res) => {},
    })
  },
  globalData: {
    userInfo: '',
  }
})
